/************************************************************
 *  * EaseMob CONFIDENTIAL
 * __________________
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of EaseMob Technologies.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from EaseMob Technologies.
 */

#import "UIViewController+Common.h"
#import "UMMobClick/MobClick.h"

const NSString *classKey = @"className";

@implementation UIViewController (Common)
- (void)setupForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    
    __weak UIViewController *weakSelf = self;
    
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [weakSelf.view addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [weakSelf.view removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}


+ (void)load{
    
    SEL origin1 = @selector(viewWillAppear:);
    SEL origin2 = @selector(viewWillDisappear:);
    
    SEL swizz1 = @selector(swiz_viewWillAppear:);
    SEL swizz2 = @selector(swiz_viewWillDisappear:);
    
    [self swizzleMethods:[self class] originalSelector:origin1 swizzledSelector:swizz1];
    [self swizzleMethods:[self class] originalSelector:origin2 swizzledSelector:swizz2];
}

+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel
{
    Method origMethod = class_getInstanceMethod(class, origSel);
    Method swizMethod = class_getInstanceMethod(class, swizSel);
    
    BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
    if (didAddMethod) {
        class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, swizMethod);
    }
}

- (void)swiz_viewWillAppear:(BOOL)animated{

    [self swiz_viewWillAppear:animated];
    Class cls = self.class;
    const char *classChar =  class_getName((cls));
    NSString *className = [NSString stringWithCString:classChar encoding:NSUTF8StringEncoding];
     //CLog(@"进className==========%@",className);
     [MobClick beginLogPageView:className];
    objc_setAssociatedObject(self, &classKey, className, OBJC_ASSOCIATION_COPY);
}
 
- (void)swiz_viewWillDisappear:(BOOL)animated{

    [self swiz_viewWillDisappear:animated];
    NSString *className = objc_getAssociatedObject(self, &classKey);
    //CLog(@"出className==========%@",className);
    [MobClick endLogPageView:className];
}


@end
